﻿<?php

use yii\helpers\Html;
use yii\helpers\Url;
use backend\widgets\GridView;
use yii\widgets\Breadcrumbs;
use yii\jui\DatePicker;
use yii\widgets\ActiveForm;
use backend\models\service\CateService as Cate;
use backend\models\service\Charge;
use yii\bootstrap\Modal;

$this->title = "书籍列表";
Modal::begin([
    'id' => 'common-modal',
    'header' => '<h4 class="modal-title"></h4>',
    'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>',
]);


$js = <<<JS
$(".modaldialog").click(function(){ 
        aUrl = $(this).attr('data-url');
        aTitle = $(this).attr('data-title');
        console.log(aTitle);
        console.log(aUrl);
        
        $($(this).attr('data-target')+" .modal-title").text(aTitle);
        $($(this).attr('data-target')).modal("show")
             .find(".modal-body")
             .load(aUrl);
        return false;
   }); 
JS;
$this->registerJs($js);

Modal::end();
?>
<div class="RightCenter clear">

    <div class="RightTime">
        <div class="Time">
            <h3>书籍列表</h3>
        </div>
    </div>
    <div class="Right_common">
        <div class="max_one">
            <h5>添加书籍</h5>
            <p><button type="file" class="btn">批量导入书籍</button></p>

            <p><?= Html::a(Html::button('手动添加书籍',['disabled' => 'disabled']), Url::to(['create']), ['target' => '_blank']) ?><p>

        </div>
        <div class="max_one">
            <h5>增值设定</h5>
            <p><?= Html::a('批量增值', Url::to(["/novel/charge-setting"]), [
                                    'id' => 'novel-charge',
                                    'class' => 'modaldialog btn btn-success zenzhi',
                                    'data-toggle' => 'modal',
                                    'data-url' => Url::to(["/novel/charge-setting"]),
                                    'data-title' => '增值设定',
                                    'data-target' => '#common-modal',
                                ])?></p>

        </div>
        <div class="max_one">
            <h5>连载更新</h5>
            <p><?= Html::a(Html::button('批量导入更新',['disabled' => 'disabled']), Url::to(['create']), ['target' => '_blank']) ?></p>
            <p><?= Html::a(Html::button('手动添加更新',['disabled' => 'disabled']), Url::to(['create']), ['target' => '_blank']) ?></p>
        </div>
        <div class="max_one max_PL">
            <h5>频道修改</h5>
            <p><?= Html::submitButton('分类/频道修改', ['class' => 'btn btn-success pindao', 'name' => 'submit-button']) ?></p>
        </div>
        <div class="max_one">
            <h5>上架操作</h5>
            <p><button class="up_novel btn btn-success">上架</button></p>
            <p><button class="down_novel btn btn-success">下架</button></p>
        </div>
        <div class="max_one">
            <h5>广告位操作</h5>
            <p><?= Html::submitButton('编辑广告位', ['class' => 'btn btn-success ads', 'name' => 'submit-button']) ?></p>
        </div>
    </div>
    <div class="Read_date" style="margin-top:20px;margin-bottom: 5px;height:60px;">
        <ul>
            <li class="Read_LisJ" style="margin-top:12.5px;">
                <div class="Seek">
                    <input type="text" class="form-control AppIN" id="exampleInputAmount" placeholder="请输入书籍名" value="<?php echo isset($select['NovelService']['novel_title']) ? $select['NovelService']['novel_title'] : '';?>">
                    <button type="button" class="btn btn-primary SS">搜索</button>
                </div>

                <div class="SX clear">筛选:
                    <select id="main">
                        <option value="">全部频道</option>
                        <option <?php echo isset($select['NovelService']['novel_main_category']) && $select['NovelService']['novel_main_category'] == 1 ? 'selected="selected"' : '';?> value="1">男生频道</option>
                        <option <?php echo isset($select['NovelService']['novel_main_category']) && $select['NovelService']['novel_main_category'] == 2 ? 'selected="selected"' : '';?> value="2">女生频道</option>
                        <option <?php echo isset($select['NovelService']['novel_main_category']) && $select['NovelService']['novel_main_category'] == '0' ? 'selected="selected"' : '';?> value="0">未分频道</option>
                    </select>

                    <select class="cate">
                        <option value="">全部分类</option>
                        <?php foreach ($category as $cate): ?>
                            <option <?php echo isset($select['NovelService']['novel_category']) && $select['NovelService']['novel_category'] == $cate['cate_id'] ? 'selected="selected"' : '';?> value="<?php echo $cate['cate_id']; ?>"><?php echo $cate['cate_name']; ?></option>
                        <?php endforeach; ?>
                    </select>
                    <select id="cate" style="display:none;">
                        <option value="">全部分类</option>
                    </select>

                    <select id="column">
                        <option value="">排序</option>
                        <option <?php echo isset($select['sort']) && str_replace('-','',$select['sort']) == 'novel_pinyin' ? 'selected="selected"' : '';?> value="1">书名</option>
                        <option <?php echo isset($select['sort']) && str_replace('-','',$select['sort']) == 'novel_id' ? 'selected="selected"' : '';?> value="2">书号</option>
                        <option <?php echo isset($select['sort']) && str_replace('-','',$select['sort']) == 'novel_author' ? 'selected="selected"' : '';?> value="3">作者</option>
                        <option <?php echo isset($select['sort']) && str_replace('-','',$select['sort']) == 'novel_word_total' ? 'selected="selected"' : '';?> value="4">字数</option>
                        <option <?php echo isset($select['sort']) && str_replace('-','',$select['sort']) == 'novel_chapter' ? 'selected="selected"' : '';?> value="5">章节</option>
                        <option <?php echo isset($select['sort']) && str_replace('-','',$select['sort']) == 'novel_type' ? 'selected="selected"' : '';?> value="6">完本</option>
                    </select>	

                    <select id="sort">
                        <option <?php echo isset($select['sort']) && strpos($select['sort'],'-') === false ? 'selected="selected"' : '';?> value="1">升序</option>
                        <option <?php echo isset($select['sort']) && strpos($select['sort'],'-') !== false ? 'selected="selected"' : '';?> value="2">降序</option>
                    </select>	

                    <select id="charge">
                        <option value="">全部增值状态</option>
                        <?php foreach ($charge as $v): ?>
                            <?php if ($v['charge_model'] == 0): ?>
                                <option <?php echo isset($select['NovelService']['novel_charge_id']) && $select['NovelService']['novel_charge_id'] == 1 ? 'selected="selected"' : '';?> value="1">免费</option>
                            <?php elseif ($v['charge_model'] == 2): ?>
                                <option <?php echo isset($select['NovelService']['novel_charge_id']) && $select['NovelService']['novel_charge_id'] == $v['charge_id'] ? 'selected="selected"' : '';?> value="<?php echo $v['charge_id']; ?>"><?php echo "全本/" . $v['charge_price'] . "阅币"; ?></option>
                            <?php else: ?>
                                <option <?php echo isset($select['NovelService']['novel_charge_id']) && $select['NovelService']['novel_charge_id'] == $v['charge_id'] ? 'selected="selected"' : '';?> value="<?php echo $v['charge_id']; ?>"><?php echo $v['charge_start_chapter'] . "章/" . $v['charge_price'] . "阅币"; ?></option>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </select>	

                    <select id="status">
                        <option value="">全部状态</option>
                        <option <?php echo isset($select['NovelService']['novel_op_status']) && $select['NovelService']['novel_op_status'] == 1 ? 'selected="selected"' : '';?> value="1">上架</option>
                        <option <?php echo isset($select['NovelService']['novel_op_status']) && $select['NovelService']['novel_op_status'] == '0' ? 'selected="selected"' : '';?> value="0">下架</option>
                    </select>	

                    <button type="button" class="btn btn-primary czS">重置</button>
                </div>
            </li>
        </ul>
    </div>
    <?=
    GridView::widget([
        'dataProvider' => $dataProvider,
        'id' => 'grid',
//        'filterModel' => $searchModel,
        'columns' => [
            [
                'class' => 'yii\grid\CheckboxColumn',
                'name' => 'id',
                'headerOptions' => ['width' => 10],
            ],
            [
                'class' => 'yii\grid\SerialColumn',
                'headerOptions' => ['width' => 10],
            ],
            [
                'attribute' => 'novel_book_number',
                'label' => '书籍编号',
                'headerOptions' => ['width' => 80],
            ],
            [
                'label' => '封面',
                'format' => [
                    'image', ['width' => '84', 'height' => '84']
                ],
                'headerOptions' => ['width' => 80],
                'value' => function ($data) {
                    return $data->novel_cover;
                }
            ],
            [
                'attribute' => 'novel_title',
                'label' => '书籍名称',
                'headerOptions' => ['width' => 80],
            ],
            [
                'attribute' => 'novel_main_category',
                'label' => '频道',
                'headerOptions' => ['width' => 80],
                'value' => function($data) {
                    switch ($data->novel_main_category){
                        case 1:
                            return '男频';
                        case 2:
                            return '女频';
                        default :
                            return '未分频道';
                    }
                }
            ],
            [
                'label' => '分类',
                'filter' => Html::activeDropDownList($searchModel, 'novel_category', Cate::items('cate_id')),
                'headerOptions' => ['width' => 80],
                'content' => function ($data, $model) {
            return Cate::item($data->novel_category);
        }
            ],
            [
                'attribute' => 'novel_author',
                'label' => '作者',
                'headerOptions' => ['width' => 30],
            ],
            [
                'label' => '状态',
                'filter' => Html::activeDropDownList($searchModel, 'novel_type', ['' => '请选择', '1' => '完结', '2' => '连载']),
                'headerOptions' => ['width' => 55],
                'value' => function($data) {
            switch ($data->novel_status) {
                case '1':
                    return '完结';
                    break;
                case '2':
                    return '连载';
                    break;
            }
        }
            ],
            [
                'attribute' => 'novel_word_total',
                'label' => '字数',
                'headerOptions' => ['width' => 80],
            ],
            [
                'label' => '章节',
                'headerOptions' => ['width' => 80],
                'value' => function($data) {
            return $data->novel_chapter . '章';
        }
            ],
            [
                'format' => 'raw',
                'label' => '增值状态',
                'headerOptions' => ['width' => 80],
                'value' => function($data){
                   $charge =  new Charge();
                    $res = $charge->getChargeModelById($data->novel_charge_id);
                    $desc = '';
                    if($res['charge_model'] == 0){
                        $desc = '免费';
                    }elseif($res['charge_model'] == 1){
                        $desc = "第".$res['charge_start_chapter']."章启</br>";
                        $desc.= $res['charge_price']."阅币/每章";
                    }elseif($res['charge_model'] == 2){
                        $desc.= $res['charge_price']."阅币/全本";
                    }
                    return $desc;
                }
            ],
            [
                'label' => '上下架状态',
                'filter' => Html::activeDropDownList($searchModel, 'novel_op_status', ['' => '请选择', '0' => '下架', '1' => '上架']),
                'headerOptions' => ['width' => 55],
                'value' => function($data) {
            switch ($data->novel_op_status) {
                case '0':
                    return '下架';
                    break;
                case '1':
                    return '上架';
                    break;
            }
        }
            ],
            [
                'class' => \yii\grid\ActionColumn::className(),
                'header' => '操作',
                'headerOptions' => ['width' => 180],
                'contentOptions' => ['class' => 'action-link'],
                'template' => '{extend}{view} {charge-setting} {cate-update}',
                'buttons' => [
                    'extend' => function ($url, $model, $key) {
                        return Html::a('分发推广', $url, [
                                    'id' => 'novel-extend',
                                    'class' => 'modaldialog',
                                    'data-toggle' => 'modal',
                                    'data-url' => $url,
                                    'data-title' => '分发推广- '.$model->novel_title,
                                    'data-target' => '#common-modal',
                                ]) . " ";
                    },
                            'view' => function ($url, $model, $key) {
                        return Html::a('查阅书籍', $url) . "</br>";
                    },
                            'charge-setting' => function ($url, $model, $key) {
                        return Html::a('增值设定修改', $url, [
                                    'id' => 'novel-charge',
                                    'class' => 'modaldialog ZZ',
                                    'data-toggle' => 'modal',
                                    'data-url' => $url,
                                    'data-title' => '增值设定',
                                    'data-target' => '#common-modal',
                                ]) . " ";
                    },
                            'cate-update' => function ($url, $model, $key) {
                        return Html::a('分类/频道修改', $url);
                    },
                        ],
                    ],
                ],
            ]);
            ?>

</div>
<script>
    var cid = '<?php echo isset($select['NovelService']['novel_category']) ? $select['NovelService']['novel_category'] : '';?>';
    $(function(){
        var main = $("#main").val();
        if(main == ''){
            $(".cate").show();
            $("#cate").hide();
        }else{
            $(".cate").hide();
            getCate(main);
        }
    });
    
    $("#main").change(function(){
        var main = $("#main").val();
        cid = '';
        if(main == ''){
            $(".cate").show();
            $("#cate").hide();
        }else{
            $(".cate").hide();
            getCate(main);
        }
    });
    
    $("#cate").change(function(){
        $("#cate").find("option[value='"+cid+"']").removeAttr('selected')
        $(".cate").find("option[value='"+cid+"']").removeAttr('selected')
        cid = '';
    });
    
    $(".cate").change(function(){
        $("#cate").find("option[value='"+cid+"']").removeAttr('selected')
        $(".cate").find("option[value='"+cid+"']").removeAttr('selected')
        cid = '';
    });
    function getCate(main){
        $("#cate").show();
        $.ajax({
            url:"/novel/ajax-get-category",
            type:"post",
            dataType:"json",
            data:{main:main},
            success:function(data){
                if(data.status == 0){
                    var str = "<option value=''>全部分类</option>";
                    var list = data.list;
                    for(i in list){
                        str += "<option value='"+list[i]['cate_id']+"'>"+list[i]['cate_name']+"</option>";
                    }
                    $("#cate").empty();
                    $("#cate").append(str);
                    $("#cate").find("option[value='"+cid+"']").attr('selected','selected')
                }
            }
        });
    }
    
    $(".SS").click(function(){
        //书名
        var name = $("#exampleInputAmount").val();
        //频道
        var main = $("#main").val();
        //分类
        var cate = $("#cate").val();
        if(!cate){
            cate = $(".cate").val();
        }
        //字段排序
        var column = $("#column").val();
        var s = '';
        switch(column){
            case '1':
                s = 'novel_pinyin';
                break;
            case '2':
                s = 'novel_id';
                break;
            case '3':
                s = 'novel_author';
                break;
            case '4':
                s = 'novel_word_total';
                break;
            case '5':
                s = 'novel_chapter';
                break;
            case '6':
                s = 'novel_type';
                break;
            default:
                break;
        }
        //升降序
        var sort = $("#sort").val();
        var o = '';
        if(sort == 2){
            o = '-';
        }
        //增值
        var charge = $("#charge").val();
        //状态
        var status = $("#status").val();
        location.href="/novel/index?NovelService[novel_title]="+name+"&NovelService[novel_main_category]="+main+"&NovelService[novel_category]="+cate+"&sort="+o+s+"&NovelService[novel_charge_id]="+charge+"&NovelService[novel_op_status]="+status
    });
    
    $(".czS").click(function(){
        location.href="/novel/index"
    });
    $(".up_novel").click(function(){
        var keys = $("#grid").yiiGridView("getSelectedRows");
        console.log(keys);
        $.post( '/novel/up-novel',{ids:keys.join(',')},function(result){
             alert(result)
             location.href="/novel/index"
        });
    });
    $(".down_novel").click(function(){
        var keys = $("#grid").yiiGridView("getSelectedRows");
        console.log(keys);
        $.post( '/novel/down-novel',{ids:keys.join(',')},function(result){
             alert(result)
             location.href="/novel/index"
        });
    });
    
    var zz_keys = '';
    $(".zenzhi").click(function(){
        zz_keys = $("#grid").yiiGridView("getSelectedRows");
    });
    
    $(".ZZ").click(function(){
        zz_keys = $(this).parent().parent('tr').attr('data-key');
        if(zz_keys == ''){
            alert('请选择需要操作的小说');
            return false;
        }else{
            
        }
    });
    
    $(".pindao").click(function(){
        zz_keys = $("#grid").yiiGridView("getSelectedRows");
        if(zz_keys == ''){
            alert('请选择需要操作的小说');
            return false;
        }
        ids = zz_keys.join(',');
        var url = "cate-update?id="+ids;
//        console.log(ids);
//        console.log(url);
        window.location.href = url ;
       
    });
    
    $(".PDFL").click(function(){
        zz_keys = $(this).parent().parent('tr').attr('data-key');
    });
    
</script>
